import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../common.dart';


class MarketDetail extends StatefulWidget {
  @override
  State<MarketDetail> createState() => _HomePageState();
}
    Timer? _timer;

class _HomePageState extends State<MarketDetail> {

  String? uuid = Get.parameters['uuid'];
  RxMap info={}.obs;

  getInfo(){
   getData(url: '/resales/info/$uuid').then((res) {
      info.value = res;
    });
  }
  goTimer(number) {
    getData(url: '/resale/checkNumber',data:  {
      "number": '$number',
    }).then((res) {

      if (res['status'] == 1) {
         _timer?.cancel();
        _timer = null;
        
          if(kIsWeb) {
              final uur=  Uri.parse('${res['pay_url']}');
              launchUrl(uur,webOnlyWindowName: '_self',);
          }else{
            toNamed('/webview?url=${res['pay_url']}&app=1&back=suu&id=${res['id']}');
          }
      }

      if (res['status'] == 0) {
         _timer?.cancel();
        _timer = null;
          print('yyyy');
        _timer = Timer.periodic(Duration(seconds: 1), (timer) {
          goTimer(number);
        });
      }

      if (res['status'] == 2) {
        Get.back();

       showTost('下单失败');
        _timer?.cancel();
        _timer = null;
      }
    });
  }

   @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getInfo();
    });
  }
showTost(text) {
  Get.dialog(
    barrierColor: Colors.transparent,
    barrierDismissible: false,
    Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
            alignment: Alignment.center,
            width: 130,
            height: 100,
            decoration: BoxDecoration(
                color: Color(0X83000000),
                borderRadius: BorderRadius.circular(15)),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CircularProgressIndicator(backgroundColor: Color(0XFFffffff)),
                Text(text,
                    style: TextStyle(fontSize: 16, color: Color(0XFFFFFFFF))),
              ],
            ))
      ],
    ),
  );
}

@override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    con(context);
    return Scaffold(
        appBar: AppBar(
          leading: buildBackButton(context),
          backgroundColor: Color(0xffffffff),
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView(
                children: [
                  Obx(()=>
                  Image.network(
                    '${info['thumb']}',
                    errorBuilder: (context, error, stackTrace) {
                               return SizedBox(
                                width: 60,
                              );
                          }
                    )
                  )
                  ,
                  SizedBox(
                    height: 15,
                  ),
                  Padding(
                      padding: EdgeInsets.symmetric(horizontal: 15),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Obx(()=>

                          Text(
                            '限量${info['sell_num']}份，流通${info['release_num']}份',
                            style: TextStyle(
                                fontSize: 14, color: Color(0XFF1F0584)),
                          ),
                          ),
                          Obx(()=>
                          Text(
                            '${info['title']}',
                            style: TextStyle(
                                fontSize: 22, color: Color(0XFF0F172B)),
                          )
                          )
                          ,
                          SizedBox(
                            height: 20,
                          ),
                          Row(
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '所在产链',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF999999)),
                                  ),
                                  Obx(()=>
                                  Text(
                                    '${info['link_name']}',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  )
                                  )
                                  ,
                                ],
                              ),
                              SizedBox(
                                width: 100,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '发售时间',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF999999)),
                                  ),
                                  Obx(()=>
                                   Text(
                                    '${info['created_at']}',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  )
                                  )
                                 ,
                                ],
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 20,
                          ),
                          Row(
                            children: [
                              // Column(
                              //   crossAxisAlignment: CrossAxisAlignment.start,
                              //   children: [
                              //     Text(
                              //       '合约地址',
                              //       style: TextStyle(
                              //           fontSize: 12, color: Color(0XFF999999)),
                              //     ),
                              //     Text(
                              //       'OX2323...5432',
                              //       style: TextStyle(
                              //           fontSize: 14, color: Color(0XFF0F172B)),
                              //     ),
                              //   ],
                              // ),
                              // SizedBox(
                              //   width: 100,
                              // ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '支付方式',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF999999)),
                                  ),
                                  Obx(()=>
                                  Text(
                                    '${info['pay_type']}',
                                    style: TextStyle(
                                        fontSize: 14, color: Color(0XFF0F172B)),
                                  )
                                  )
                                  ,
                                ],
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 30,
                          ),
                          Text(
                            '藏品简介',
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Obx(()=>
                            Column(
                              children: [
                                 if(info.isNotEmpty)
                                 for(var item in info['images'])
                                  Image.network('$item'),
                              ],
                            )
                          )
                         ,
                          SizedBox(
                            height: 10,
                          ),
                          Text(
                            '购买须知',
                            style: TextStyle(
                                fontSize: 18, fontWeight: FontWeight.bold),
                          ),
                          SizedBox(
                            height: 5,
                          ),
                          Text(
                            '数字藏品交易仅限于18周岁(不含)以上、60周岁(含)以下，并已完成实名认证的中国大陆用户购买。由于不同属性的数字艺术有不同的二次交易规则，请您仔细阅读并同意相关规则后再进行相应操作，以避免不必要的损失。 请注意，一旦数字藏品完成出售，不支持退货在购买之前，请仔细考虑您的购买决策。根据我国相关的法律要求，特定条件下的数字藏品的二次交易不支持任何形式的变相炒作。我们坚决反对数字藏品炒作、场外交易、欺诈，以及其他非法侵权行为。请您甄别上述涉嫌网络欺诈的行为并了解相关风险。',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF999999)),
                          ),
                          SizedBox(
                            height: 15,
                          ),
                        ],
                      )),
                ],
              ),
            ),
            Align(
                alignment: Alignment.bottomLeft,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 15),
                      decoration: BoxDecoration(
                        color: Color(0XFFFFFFFF),
                      ),
                      child: Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        height: 60,
                        decoration: BoxDecoration(
                            color: Color(0XFF0F172B),
                            borderRadius: BorderRadius.circular(60)),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 25,
                            ),
                            Text(
                              '￥',
                              style: TextStyle(
                                  fontSize: 16, color: Color(0XFFFFFFFF)),
                            ),
                            Obx(()=>
                            info.isNotEmpty?
                            Text(
                              '${(info['money_price']/100)}',
                              style: TextStyle(
                                  fontSize: 24, color: Color(0XFFFFFFFF)),
                            ):SizedBox()
                            )
                            ,
                            Expanded(child: SizedBox()),
                            GestureDetector(
                              onTap: (){
                                if(info['status']==5)return;

                                postData(url: '/resale/buy', data: {
                                  "product_id":'${info['product_id']}',
                                  "resale_id":'${info['resale_id']}',
                                  "url":'back.html?app=1&web=back',
                                }).then((res){
                                  if(res!=null){
                                    showTost('正在锁单中...');
                                    _timer?.cancel();
                                       goTimer(res['number']);
                                    //  toNamed('/webview');
                                  }
                                });

                                // toNamed('/web_tabs/details/settlement')
                              },
                              child: 
                              Obx(()=>
                                    Text(
                                 info['status']==5?'锁定中':'立即购买',
                                style: TextStyle(
                                    fontSize: 18, color: Color(0XFFFFFFFF)),
                              ),
                              )
                          
                            ),
                            SizedBox(
                              width: 25,
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ))
          ],
        ));
  }
}
